package com.example.csmall.commons.web;

import com.example.csmall.commons.ex.ServiceException;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 向前端响应结果的类
 */

@Data
@Accessors(chain = true)
public class JsonResult<T> implements Serializable {
    private Integer state;
    private String message;
    private T data;

    public static JsonResult<Void> ok() {
        return ok(null);
    }

    public static <T> JsonResult<T> ok(T data) {
        JsonResult<T> jsonResult = new JsonResult<>();
        jsonResult.setState(ServiceCode.OK.getValue());
        jsonResult.setData(data);
        return jsonResult;
    }

    public static JsonResult<Void> fail(ServiceException e) {
        return fail(e.getServiceCode(), e.getMessage());
    }

    public static JsonResult<Void> fail(ServiceCode state, String message) {
        JsonResult<Void> jsonResult = new JsonResult<>();
        jsonResult.setState(state.getValue());
        jsonResult.setMessage(message);
        return jsonResult;
    }

//    public JsonResult setState(Integer state) {
//        this.state = state;
//        return this;
//    }
//
//    public JsonResult setMessage(String message) {
//        this.message = message;
//        return this;
//    }
//
//    public Integer getState() {
//        return state;
//    }
//
//    public String getMessage() {
//        return message;
//    }
}
